package name.bingo.bing.pay.utils;
/**
 * create by yanbing on 2018/9/13 13:48
 */

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 订单相关工具类
 * @auther: yanbing
 * @date: 2018/9/13 13：48
 **/
public class OrderUtils {
    /**
    * 订单号生成规则，time:12+random:3
    * @return:
    */
    public static String getOrderId(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmm");
        String dateName=sdf.format(new Date());
        Random random=new Random();
        int x=random.nextInt(999-100+1)+100;//[0,999)
        String randomName=String.valueOf(x);
        String orderId=dateName+randomName;
        return orderId;
    }
    /**
     *  获取request数据,数组改为字符串
     * @param:Map
     * @return:Map
     */
    public static Map changeMapData(Map requestParams){
        Map<String,String> params = new HashMap<String,String>();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。
            try {
                valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            params.put(name, valueStr);
        }
        return params;
    }

}
